package moodish.comm;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

import moodish.dummy.ServerCommDummy;

/**
 * Thread started by the server to listen new Connections.
 * This will execute until the server shutdown.
 * 
 * @author Diogo Medeiros
 * @version 1
 *  
 */
public class ListenConnectionsFromClient implements Runnable {
	private Socket clientSocket;
	private ServerSocket serverSocket;
	private ServerCommDummy scm;

	public ListenConnectionsFromClient(Socket clientSocket, ServerSocket serverSocket, ServerCommDummy scm)
	{
		this.clientSocket=clientSocket;
		this.serverSocket=serverSocket;
		this.scm=scm;
	}
	
	@Override
	public void run() {
		// TODO Auto-generated method stub
		try {
			while (true) {
				clientSocket = serverSocket.accept();
				ListenMessagesFromClient sSocket = new ListenMessagesFromClient(clientSocket, scm);
				Thread newClient = new Thread(sSocket);
				newClient.start();
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println("Could not listen on this port");
			System.exit(-1);
		}
	}
	/** 
	 * This Method will return the ServerCommDummy class.
	 * @return The ServerCommDummy class.
	 */
	public ServerCommDummy getScm() {
		return scm;
	}

}
